package MWClient;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * <pre>
 * @Package : MWClient
 * @File    : ClientReceiver.java
 * <pre>
 *
 * @Author  : 차재우
 * @Date    : 2013. 7. 22.
 * @Version :
 */
class ClientReceiver implements Runnable {
	private Socket sock = null;
	private DataInputStream in = null;
	private BufferedReader br = null;
	private SerialQueue serialSendQueue = null;

	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 * @param socket
	 */
	public ClientReceiver(Socket socket) {
		this.sock = socket;
		this.serialSendQueue = MWClient.sendQueue;

		try {
			in = new DataInputStream(sock.getInputStream());
			br = new BufferedReader(new InputStreamReader(in));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <pre>
	 * @Comment : 클라이언트 수신 스레드 시작 루틴
	 * <pre>
	 *
	 * @Method  : run
	 */
	public void run() {
		while (in != null) {
			try {
				String recvStr = br.readLine();
				serialSendQueue.Push(recvStr.getBytes());
				
				System.out.println("\n***************************************************************************************************************************************");
				System.out.println("Mobile <------------------- Middleware");
				System.out.println(recvStr);
				System.out.println("Serial send queue size : " + serialSendQueue.GetQueueSize());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
